<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/3/8
 * Time: 9:59
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\api\model\Skill as SkillModel;
use app\api\model\UserCategory;
use think\Db;
use think\Log;

class Skill extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /**
     * 补全技能信息接口
     */
    public function res_info()
    {
        $res_info = $this->request->param();

        /*$skills = [
            15 => ["产品经理"],
            20 => ["es", "c"],
            21 => ["java", "c++"]
        ];

        $res_info = [
            'user_id' => 1,
            'skilljson' => $skills
        ];*/

        if (!$res_info) {
            $this->error(__('请传数据'));
        }

        //modify by guojing 2020-04-05
        /*if ($this->auth->id <= 0) {
            $this->error(__('请先登录'), ['res_info' => $res_info, 'uid' => $this->auth->id, 'user data' => $this->auth->getUser()]);
        }*/
        if (!isset($res_info['skilljson']) || !$res_info['skilljson']) {    //没有技能标签
            $this->error(__('技能标签为空'));
        }
        if (!is_array($res_info['skilljson'])) {
            $this->error(__('技能标签参数格式错误'));
        }

        //技能对应的人才id
        $categoryIds = [];
        if ($res_info['skilljson']) {
            foreach ($res_info['skilljson'] as $categoryId => $infoVal) {
                if (is_array($infoVal)) {
                    $categoryIds = array_merge($categoryIds, array_keys($infoVal));
                } else {
                    $this->error(__('技能标签参数格式错误'));
                }
            }
        }
        $categoryIds = array_unique($categoryIds);
        if (!$categoryIds) {
            $this->error(__('岗位标签参数格式错误'));
        }

        $skill = SkillModel::findInfoByCondition(['user_id' => $res_info['user_id']]);
        if ($skill) {   //修改
            $data = [
                'skilljson' => json_encode($res_info['skilljson'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
            ];
            SkillModel::saveData($skill['id'], $data);

            // 更新用户人才关联表
            $oldSkilljson = $skill['skilljson'] ? json_decode($skill['skilljson'], true) : [];
            $oldCategoryIds = [];
            if ($oldSkilljson) {
                foreach ($oldSkilljson as $categoryId => $infoVal) {
                    $oldCategoryIds = array_merge($oldCategoryIds, array_keys($infoVal));
                }
            }
            $oldCategoryIds = array_unique($oldCategoryIds);

            $addCategoryIds = array_diff($categoryIds, $oldCategoryIds);
            $delCategoryIds = array_diff($oldCategoryIds, $categoryIds);
            if ($addCategoryIds) {  // 有新添加的人才
                foreach ($addCategoryIds as $acid) {
                    $data = [
                        'uid' => $res_info['user_id'],
                        'category_id' => $acid
                    ];
                    UserCategory::addRecord($data);
                    unset($data);
                }
            }
            if ($delCategoryIds) {
                UserCategory::delByCondition(['uid' => $res_info['user_id'], 'category_id' => ['in', $delCategoryIds]]);
            }
        } else {    //添加
            $data = [
                //'user_id' => $this->auth->id,
                'user_id' => $res_info['user_id'],
                'skilljson' => json_encode($res_info['skilljson'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
            ];
            $skill = SkillModel::addRecord($data);
            if (!$skill) {
                $this->error(__('添加失败'));
            }

            //写入用户人才表
            if ($categoryIds) {
                foreach ($categoryIds as $cid) {
                    $data = [
                        'uid' => $res_info['user_id'],
                        'category_id' => $cid
                    ];
                    UserCategory::addRecord($data);
                    unset($data);
                }
            }
        }

        //添加成功
        $this->success('success', []);

        //file_put_contents('1.txt', '$res_info');

//        $res_info = [
//            ['user_id' => 2, 'skilljson' => "{\"java高级开发\":\"java,linux,C#\"}"],
//            ['user_id' => 2, 'skilljson' => "{\"java高级开发\":\"java,linux,C#\"}"]
//        ];

//
//        $data = [
//            'user_id' => '47',
//            'skilljson' =>
//                "{'项目经理' => ['PS', 'AE']}",
//            "{'UI设计师' => ['PS', 'AE']}",
//            'createtime' => 1584512801,
//        ];
//        halt($data);
        //$arr = [];
        
//        halt($arr);
//        $user = new \app\api\model\Skill;
//        $res = $user->saveAll($arr);
//        if ($res !== null) {
//            $this->success('success', $res);
//        } else {
//            $this->error('error');
//        }
    }

    /**
     * 查找专家名下的技能标签
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show()
    {
        $user = new \app\api\model\Skill;
        $res = $user->where('user_id', $this->auth->id)->select();
        $this->success('success', $res);
    }

    public function index()
    {
        return 1;
    }

}